package com.gscitysfy.cus.drain.modules.warning.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cus.drain.modules.common.entity.DrainInspectionMnFile;
import com.gscitysfy.cus.drain.modules.inspection.entity.DrainInspectionMnJob;
import com.gscitysfy.cus.drain.modules.inspection.entity.DrainInspectionMnPlan;
import com.gscitysfy.cus.drain.modules.inspection.entity.DrainInspectionRlUser;
import com.gscitysfy.cus.drain.modules.inspection.mapper.DrainInspectionMnJobMapper;
import com.gscitysfy.cus.drain.modules.inspection.mapper.DrainInspectionMnProblemMapper;
import com.gscitysfy.cus.drain.modules.inspection.mapper.DrainInspectionRlTrackMapper;
import com.gscitysfy.cus.drain.modules.inspection.service.IDrainInspectionMnFileService;
import com.gscitysfy.cus.drain.modules.inspection.service.IDrainInspectionMnJobService;
import com.gscitysfy.cus.drain.modules.inspection.vo.DrainInspectionMnJobVo;
import com.gscitysfy.cus.drain.modules.warning.entity.DrainWaringSystem;
import com.gscitysfy.cus.drain.modules.warning.mapper.DrainWaringSystemMapper;
import com.gscitysfy.cus.drain.modules.warning.service.IDrainWaringSystemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2022-10-20 19:15:57
 */
@Service
public class DrainWaringSystemServiceImpl extends ServiceImpl<DrainWaringSystemMapper, DrainWaringSystem> implements IDrainWaringSystemService {
    @Autowired
    DrainWaringSystemMapper drainWaringSystemMapper;
    @Autowired
    IDrainInspectionMnFileService drainInspectionMnFileService;

    @Override
    public Result<Object> getByPage(Page<DrainWaringSystem> page, DrainWaringSystem drainWaringSystem) {
            IPage<DrainWaringSystem> drainWaringSystemList =drainWaringSystemMapper.getPageList(page,drainWaringSystem);
        return Result.content(drainWaringSystemList);
    }

    @Override
    public Result<Object> getListByQuery(DrainWaringSystem drainWaringSystem) {
        if(drainWaringSystem.getCmKid()!=null){
            List<DrainWaringSystem> drainWaringSystemList =drainWaringSystemMapper.getPageList(drainWaringSystem);
            if(CollectionUtils.isEmpty(drainWaringSystemList)){
                return Result.content(null);
            }else{
                return Result.content(drainWaringSystemList.get(0));
            }
        }else{
            List<DrainWaringSystem> drainWaringSystemList =drainWaringSystemMapper.getPageList(drainWaringSystem);
            return Result.content(drainWaringSystemList);

        }

    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Result<Boolean> del(DrainWaringSystem drainWaringSystem) {
        drainWaringSystem = drainWaringSystemMapper.selectById(drainWaringSystem.getCmKid());
        drainWaringSystem.setCmUsable(true);
        Long fileId = drainWaringSystem.getWarningFileId();
        drainWaringSystemMapper.updateById(drainWaringSystem);
        if(fileId!=null){
            DrainInspectionMnFile drainInspectionMnFile = new DrainInspectionMnFile();
            drainInspectionMnFile.setCmKid(fileId);
            drainInspectionMnFile.setCmUsable(true);
            drainInspectionMnFileService.updateById(drainInspectionMnFile);
        }
        return Result.content(true);
    }


    /**
     * 新增或者修改预警机制
     * @param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Result<Boolean> addOrUpdate(DrainWaringSystem drainWaringSystem) {

        return Result.content(this.saveOrUpdate(drainWaringSystem));
    }
}
